﻿using System;
using System.Collections.Generic;
using System.Text;
using HZ.BidSystem.Model;
using System.Web;
using System.Linq;

namespace HZ.BidSystem.BLL
{
    public class BLL_Cache
    {
        public static Dictionary<Int32, Company> CompanyCache { get; set; }


        // public static Dictionary<Int32, GoodsInfo> GoodsCache { get; set; }


        static BLL_Cache()
        {
            string sql = "SELECT * FROM TB_Company";
            var list = HZ.Data.DAL_Helper.CommonFillList<Company>(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql));

            CompanyCache = new Dictionary<int, Company>();
            foreach (var item in list)
            {
                CompanyCache.Add(item.CompanyId.Value, item);
            }
            //sql = "SELECT * FROM GoodsInfo";

            //var list2 = HZ.Data.DAL_Helper.CommonFillList<GoodsInfo>(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql));

            //GoodsCache = new Dictionary<Int32, GoodsInfo>(8196);
            //foreach (var item in list2)
            //{
            //    GoodsCache.Add(item.GoodsId.Value, item);
            //}
        }
        /// <summary>
        /// 获取当前上下文报价密码
        /// </summary>
        public static string BidPwd { get { return HttpContext.Current.Session["BidPwd"].ToString(); } }


        #region 强制下线代码
        private static IDictionary<int, DateTime> _ForbidLogin = new Dictionary<int, DateTime>();
        /// <summary>
        /// 清空报价密码后强制要求重新登录数据
        /// </summary>
        public static IDictionary<int, DateTime> ForbidLogin
        {
            get
            {
                return _ForbidLogin;
            }
        }
        #endregion

        #region 在线统计代码
        private static Dictionary<int, OnLineModel> _OnLineCache = new Dictionary<int, OnLineModel>();

        /// <summary>
        /// 获取当前在线用户列表
        /// </summary>
        /// <returns></returns>
        public static IList<OnLineModel> GetBindList()
        {
            IList<OnLineModel> list = new List<OnLineModel>();
            IList<int> removekeys = new List<int>();
            foreach (var item in _OnLineCache)
            {
                if (item.Value.LastActiveTime.Value < DateTime.Now.AddMinutes(-20))
                {
                    removekeys.Add(item.Key);
                }
                else
                {
                    list.Add(item.Value);
                }
            }


            Remove(removekeys);
            list = list.OrderByDescending<OnLineModel, DateTime>((model) => { return model.LastActiveTime.Value; }).ToList();
            return list;
        }

        static object lockhelp = new object();
        /// <summary>
        /// 清空不在线的用户
        /// </summary>
        /// <param name="keylist"></param>
        public static void Remove(IList<int> keylist)
        {
            lock (lockhelp)
            {
                foreach (var item in keylist)
                {
                    if (_OnLineCache.ContainsKey(item))
                    {
                        _OnLineCache.Remove(item);
                    }
                }
            }
        }

        /// <summary>
        /// 注销下线
        /// </summary>
        public static void LogOff()
        {
            lock (lockhelp)
            {
                if (CurrentAccount.Account != null && _OnLineCache.ContainsKey(CurrentAccount.Account.AccountId.Value))
                {

                    _OnLineCache.Remove(CurrentAccount.Account.AccountId.Value);
                }
            }
        }
        /// <summary>
        /// 设置在线情况
        /// </summary>
        /// <param name="companyid"></param>
        /// <param name="model"></param>
        public static void SetOnlineModel(int companyid, OnLineModel model)
        {
            if (_OnLineCache.ContainsKey(companyid)) // 存在该实体
            {
                var cumodel = _OnLineCache[companyid];
                cumodel.Active = model.Active;
                cumodel.CompanyName = CurrentAccount.OrgName;
                cumodel.IPAddress = CurrentAccount.ClientIP;
                cumodel.LastActiveTime = DateTime.Now;
                cumodel.UserCode = CurrentAccount.Account.AccountName;
                _OnLineCache[companyid] = cumodel;
            }
            else
            {
                var cumodel = new OnLineModel();
                cumodel.Active = model.Active;
                cumodel.CompanyName = CurrentAccount.OrgName;
                cumodel.IPAddress = CurrentAccount.ClientIP;
                cumodel.LastActiveTime = DateTime.Now;
                cumodel.UserCode = CurrentAccount.Account.AccountName;

                if (_OnLineCache.Count + 1 > MaxCount)
                {
                    MaxCount = _OnLineCache.Count + 1;
                    MaxCountDataTime = DateTime.Now;
                }
                _OnLineCache[companyid] = cumodel;
            }
        }

        #endregion

        #region 最高峰统计

        /// <summary>
        /// 最高
        /// </summary>
        public static int MaxCount { get; set; }
        public static DateTime? MaxCountDataTime { get; set; }


        #endregion




    }
}
